package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //点赞/评论/喜欢通用
    public Integer save(Comment comment) {
        // 1 往quanzi_comment表中插入一条comment数据
        comment.setCreated(System.currentTimeMillis());
        //发布时间\
        //评论内容类型： 1-对圈子动态操作 2-对视频操作 3-对评论操作
        Integer pubType = comment.getPubType();

        if(pubType == 1) {
            //从publish获取发布动态的这个人id
            Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
            comment.setPublishUserId(publish.getUserId()); //给谁做的点赞、喜欢 评论
        }else if(pubType == 2){
            Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
            comment.setPublishUserId(video.getUserId()); //给谁做的点赞、喜欢 评论
        }else {

        }
        mongoTemplate.insert(comment);

        // 2 修改quanzi_publish表中的点赞量+1 likeCount

        if(pubType == 1){
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(),1); //点赞+1 喜欢+1 评论
            mongoTemplate.updateFirst(query,update,Publish.class);
        }else if(pubType == 2){
            // 2 修改quanzi_video表中的点赞量或者评论量+1 likeCount
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(),1); //点赞+1 喜欢+1 评论
            mongoTemplate.updateFirst(query,update,Video.class);
            System.out.println("修改成功");
            //修改后
        }else {

        }

        //3 查询点赞、喜欢 评论的数量返回
                            //统计quanzi_comment的publishid和commntType

        Query countQuery=new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("pubType").is(comment.getPubType()).and("commentType").is(comment.getCommentType()));
        Long count = mongoTemplate.count(countQuery, Comment.class);
        System.out.println("chacunshuliang"+count.intValue());
        return count.intValue();
    }

    //取消点赞
    public Integer remove(Comment comment) {
        //1 往quanzi_comment表中删除一条comment数据
        Query query = new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("userId").is(comment.getUserId())
        .and("pubType").is(comment.getPubType())
        .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query,Comment.class);


        if(comment.getPubType() == 1){
            // 2 修改quanzi_publish表中的点赞量-1 likeCount
            Query query2 = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(),-1); //点赞+1 喜欢+1 评论
            mongoTemplate.updateFirst(query2,update,Publish.class);
        }else if(comment.getPubType() == 2){
            // 2 修改quanzi_video表中的点赞量-1 likeCount
            Query query2 = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(),-1); //点赞+1 喜欢+1 评论
            mongoTemplate.updateFirst(query2,update,Video.class);
        }


        //3 查询点赞、喜欢 评论的数量返回
                    //统计quanzi_comment的publishid和commntType
        Query countQuery=new Query(Criteria.where("publishId").is(comment.getPublishId())
                .and("pubType").is(comment.getPubType()).and("commentType").is(comment.getCommentType()));
        Long count = mongoTemplate.count(countQuery, Comment.class);
        System.out.println(count);
        return count.intValue();

    }

    //分页查询评论列表
    public PageBeanVo findCommentPage(Integer page, Integer pagesize, String publishId,Integer pubType) {

        //条件
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId))
        .and("commentType").is(2).and("pubType").is(pubType));
        //排序
        query.with(new Sort(Sort.Direction.DESC,"created"));
        //分页
        query.skip((page-1)*pagesize).limit(pagesize);
        //查询
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        //封装并返回pageBeanVo
        return new PageBeanVo(page,pagesize,count,list);
    }
}
